#!/bin/bash

username=tpcc_1p1s_nopart
disks=(/usr1 /usr2 /usr3 /usr4)




gausshome=/${disks[0]}/$username/package
data=/${disks[0]}/$username/data
xlog=/${disks[1]}/$username/pg_xlog
tbs2=/${disks[2]}/$username/tablespace2
tbs3=/${disks[3]}/$username/tablespace3
data_bak=/${disks[0]}/$username/data_bak
xlog_bak=/${disks[1]}/$username/xlog_bak
tbs2_bak=/${disks[2]}/$username/tbs2_bak
tbs3_bak=/${disks[3]}/$username/tbs3_bak



function somecmds
{
    echo ""
    echo "some useful commands: "
    echo "  gs_initdb -D $GAUSSDATA -w Gauss@123 --nodename='sgnode'"
    echo "  numactl -C 0-28,32-60,64-92,96-124 gs_ctl start -D $data -M primary"
    echo "  numactl -C 0-25 gs_ctl start -D $data -M standby"
    echo "  numactl -C 1-28,32-60,64-92,96-124,128-156,160-188,192-220,224-252 --preferred=0 gs_ctl restart -D $datadir  -Z single_node"
    echo "  numactl -C 0-19,32-51,64-83,96-115 ./runBenchmark.sh  1p1s_5min_nopart.pg"
}


function clear_mem
{
    echo 'clear os memory .... ' -n
    ipcs -m | awk '$2 ~/[0-9]+/ {print $2}' | while read s; do ipcrm -m $s; done
    ipcs -s | awk '$2 ~/[0-9]+/ {print $2}' | while read s; do ipcrm -s $s; done
    echo 3 > /proc/sys/vm/drop_caches
    
    if [ $? -eq 0 ]; then
        echo "ok"
    else
        echo "falied"
    fi
    
    free -g
}
if   [ "$1" = "clear-mem" ]; then
    clear_mem
    exit 0
elif [ "$1" = "--help" ] ; then
    echo 'clear-mem      : clean memory'
fi


function env_init
{

    Cwd=`cd $(dirname $0);pwd`
    echo "使用的网卡为:$intf"
    combined=12
    
    echo '停止部分service'
    service sysmonitor stop
    service irqbalance stop
    service rsyslog stop
    service firewalld stop
    
    echo 'cancal CPU numa balancing'
    echo 0 > /proc/sys/kernel/numa_balancing
    
    echo 'disable hugepage'
    echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
    echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
    
    echo '磁盘队列配置'
    echo none > /sys/block/nvme0n1/queue/scheduler
    echo none > /sys/block/nvme1n1/queue/scheduler
    echo none > /sys/block/nvme2n1/queue/scheduler
    echo none > /sys/block/nvme3n1/queue/scheduler
    
    echo '网卡配置'
    intf=$1
    ethtool -G ${intf} rx 1024 tx 1024
    ethtool -K ${intf} lro on
    ethtool -L ${intf} combined ${combined}
    sh ${Cwd}/bind_58.sh
    sh ${Cwd}/irq_check.sh
    
    ifconfig ${intf} mtu 1500
    
    clear_mem
}
if   [ "$1" = "env-init" ]; then
    env_init
    exit 0
elif [ "$1" = "--help" ] ; then
    echo 'env-init      : configure all os env, such as service, hugepage, ethtool, irq-bind...'
fi


function build
{
    package=$1
    if   [ "$package" = "" ] ; then
        echo 'build need a package'
    fi
    
    useradd $username
    passwd $username

    mkdir /${disks[0]}/$username
    mkdir /${disks[1]}/$username
    mkdir /${disks[2]}/$username
    mkdir /${disks[3]}/$username

    mkdir /home/$username/package
    tar -zxvf $package -C /home/$username/package
    mv $package /home/$username/package


    echo 'export USERDATA=/${disks[0]}/$username                   ' >> /home/$username/package/env.source
    echo 'export GAUSSHOME=/home/$username/package                 ' >> /home/$username/package/env.source
    echo 'export GAUSSDATA=\$USERDATA/data                         ' >> /home/$username/package/env.source
    echo 'export PATH=\$GAUSSHOME/bin:\$PATH                       ' >> /home/$username/package/env.source
    echo 'export LD_LIBRARY_PATH=\$GAUSSHOME/lib:\$LD_LIBRARY_PATH ' >> /home/$username/package/env.source

    echo '                                         ' >> /home/$username/.bashrc
    echo 'source /home/$username/package/env.source' >> /home/$username/.bashrc

    source /home/$username/package/env.source
    gs_initdb -D $GAUSSDATA -w Gauss@123 --nodename='sgnode'

    chown $username:$username /home/$username         -R
    chown $username:$username /${disks[0]}/$username  -R
    chown $username:$username /${disks[1]}/$username  -R
    chown $username:$username /${disks[2]}/$username  -R
    chown $username:$username /${disks[3]}/$username  -R
    
}


function dispatch
{
    gs_ctl stop -D $data
    mv $data $data_bak
    cd $data_bak
    echo -n "dispatch data xlog tbs2 tbs3 ...... "
    mv pg_xlog $xlog_bak &
    mv pg_location/tablespace2 $tbs2_bak &
    mv pg_location/tablespace3 $tbs3_bak &
    wait
    echo 'ok'
    
}


function restore
{
    echo -n "rm data xlog tbs2 tbs3 ...... "
    rm -rf $data $xlog $tbs2 $tbs3
    echo 'ok'
    
    echo -n "copy data xlog tbs2 tbs3 ...... "
    cp   -r $data_bak $data &
    cp   -r $xlog_bak $xlog &
    cp   -r $tbs2_bak $tbs2 &
    cp   -r $tbs3_bak $tbs3 &
    wait
    echo 'ok'
    
    chown -R $username:$username /${disks[0]}/$username
    chown -R $username:$username /${disks[1]}/$username
    chown -R $username:$username /${disks[2]}/$username
    chown -R $username:$username /${disks[3]}/$username

    ln -svf $xlog $data/pg_xlog
    ln -svf $tbs2 $data/pg_location/tablespace2
    ln -svf $tbs3 $data/pg_location/tablespace3
    
    
    echo 'clear os memory ......' -n
    
    ipcs -m | awk '$2 ~/[0-9]+/ {print $2}' | while read s; do ipcrm -m $s; done
    echo 3 > /proc/sys/vm/drop_caches
    
    echo 'ok'
    free -g
}

if   [ "$1" = "build" ] || [ "$1" = "init" ]; then
    build $2
elif [ "$1" = "dispatch" ] ; then
    dispatch
elif [ "$1" = "restore" ] ; then
    restore
fi


somecmds